<template>
	<view class="container" style="overflow: hidden;">
		<!-- tab栏 -->
		<view class="header f-28 col-3">
			<view @tap.stop="bindHeaderTap" :class="{ active: dataType === 'all' }" data-type="all"><text>全部</text></view>
			<view @tap.stop="bindHeaderTap" :class="{ active: dataType === 'payment' }" data-type="payment"><text>待付款</text></view>
			<view @tap.stop="bindHeaderTap" :class="{ active: dataType === 'delivery' }" data-type="delivery"><text>待发货</text></view>
			<view @tap.stop="bindHeaderTap" :class="{ active: dataType === 'received' }" data-type="received"><text>待收货</text></view>
			<view @tap.stop="bindHeaderTap" :class="{ active: dataType === 'comment' }" data-type="comment"><text>待评价</text></view>
		</view>
		<mescroll-body :top="88" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
			<view class="goods-detail-box" wx:if="list.data.length > 0">
				<view class="goods-detail-item " v-for="(item, index) in list" :key="index" @tap.stop="navigateToDetail" :data-id="item.order_id">
					<view class="order-box b-f">
						<view class="order-header dis-flex">
							<view class="flex-box t-l">
								<text class="cont">{{ item.state_text }}</text>
							</view>
							<view class="flex-box t-r order-time">
								<text>{{ item.create_time }}</text>
							</view>
						</view>
					</view>

					<view class="order-box order-img_list">
						<view class="order-shop">
							<view class="order-list-box dis-flex">
								<view class="left">
									<view class="dis-flex">
										<view class="order-shop-left" v-for="(good, gIndex) in item.goods" :key="gIndex"><image :src="good.image.file_path"></image></view>
									</view>
								</view>
								<view class="right dis-flex flex-x-center flex-y-center">
									<text class="goods-number">共{{ item.goods.length }}件</text>
									<text class="suifont sui-xiangyoujiantou user-orderJtou"></text>
								</view>
							</view>
						</view>
					</view>
					<view class="order-box b-f">
						<view class="order-btn dis-flex">
							<view class="order-left">
								<view class="text">
									<text>合计：</text>
									<text class="col-m">￥{{ item.pay_price }}</text>
								</view>
							</view>
							<view class="order-right">
								<view class="btn-group dis-flex" v-if="item.order_status.value != 20">
									<!-- 未支付取消订单 -->
									<view v-if="item.pay_status.value == 10"><view @tap.stop="cancelOrder" class="btn-default" :data-id="item.order_id">取消</view></view>
									<!-- 已支付取消订单 -->
									<template v-if="item.order_status.value != 21">
										<view v-if="item.pay_status.value == 20 && item.delivery_status.value == 10"><view @tap.stop="cancelOrder" class="btn-default" :data-id="item.order_id">申请取消</view></view>
										<!-- 订单核销码 -->
										<view v-if="item.pay_status.value == 20 && item.delivery_type.value == DeliveryTypeEnum.EXTRACT.value && item.delivery_status.value == 10">
											<view @tap.stop="onExtractQRCode" class="btn-main" :data-id="item.order_id">
												<text class="suifont sui-qr-extract"></text>
												<text class="m-l-10">核销码</text>
											</view>
										</view>
									</template>
									<template v-else>
										<view class="f-28 col-8">取消申请中</view>
									</template>
									<!-- 订单付款 -->
									<view v-if="item.pay_status.value == 10"><view @tap.stop="onPayOrder" class="btn-main" :data-id="item.order_id">付款</view></view>
									<!-- 确认收货 -->
									<view v-if="item.delivery_status.value == 20 && item.receipt_status.value == 10"><view @tap.stop="receipt" class="btn-main" :data-id="item.order_id">确认收货</view></view>
									<!-- 订单评价 -->
									<view v-if="item.order_status.value == 30 && item.is_comment == 0"><view @tap.stop="comment" class="btn-default" :data-id="item.order_id">评价</view></view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
		<!-- 支付方式弹窗 -->
		<payment-popup source="order" v-if="showPayPopup" @close="onTogglePayPopup" @confirm="recharge"></payment-popup>
		<!-- 核销码弹出层 -->
		<popup :show="showQRCodePopup" @close="onToggleQRCodePopup">
			<view class="pop-qrcode pop-example__container">
				<view class="pop-title"><text class="f-30">核销二维码</text></view>
				<view class="pop-content"><image :src="QRCodeImage"></image></view>
			</view>
		</popup>
	</view>
</template>

<script>
import Tools from '@/utils/Tools.js';
import popup from '@/components/popup/index.vue';
import paymentPopup from '@/components/custom/payment-popup.vue';
// 枚举类：发货方式
import DeliveryTypeEnum from '@/utils/enum/DeliveryType.js';

// 枚举类：支付方式

import Payment from '@/utils/Payment.js';
import { HTML_DOMAIN } from '@/constant/Config.js';

import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';
export default {
	mixins: [MescrollMixin],
	components: {
		popup,
		paymentPopup
	},
	data() {
		return {
			dataType: 'all',
			scrollHeight: null,
			list: [],
			DeliveryTypeEnum, // 配送方式

			showQRCodePopup: false, // 核销码弹窗显示隐藏
			QRCodeImage: '', // 核销码图片，
			showPayPopup: false
		};
	},
	onLoad(options) {
		this.dataType = options.type || 'all';
		this.setListHeight();
	},
	methods: {
		upCallback(mescroll) {
			console.log(mescroll);
			this.$u.api.getOrderList(mescroll.num, this.dataType).then(res => {
				let resList = res.data.list;
				let dataList = this.list;
				if (mescroll.num > 1) {
					this.list.push(...resList.data);
				} else {
					this.list = resList.data;
				}
				this.mescroll.endBySize(resList.data.length, resList.total);
			});
		},
		/**
		 * 切换标签
		 */
		bindHeaderTap(e) {
			this.dataType = e.currentTarget.dataset.type;
			this.list = { list: { data: [] } };
			// 获取订单列表
			this.$nextTick(() => {
				this.mescroll.resetUpScroll(true);
			});
		},
		/**
		 * 设置商品列表高度
		 */
		setListHeight() {
			let systemInfo = uni.getSystemInfoSync();
			let scrollHeight = systemInfo.windowHeight - uni.upx2px(88); // swiper高度
			this.scrollHeight = scrollHeight;
		},
		/**
		 * 获取订单列表
		 */
		getOrderList(mescroll) {},
		/**
		 * 取消订单
		 */
		cancelOrder(e) {
			let order_id = e.currentTarget.dataset.id;
			uni.showModal({
				title: '友情提示',
				content: '确认要取消该订单吗？',
				success: o => {
					if (o.confirm) {
						this.$u.api.cancelOrder(order_id).then(res => {
							this.getOrderList(this.dataType);
						});
					}
				}
			});
		},
		/**
		 * 点击付款按钮
		 */
		onPayOrder(e) {
			// 记录订单id
			this.payOrderId = e.currentTarget.dataset.id;

			// 显示支付方式弹窗
			this.onTogglePayPopup();
		},
		/**
		 * 选择支付方式
		 */
		recharge(data) {
			this.payment(this.payOrderId, data.pay_type);
		},
		/**
		 * 显示/隐藏支付方式弹窗
		 */
		onTogglePayPopup() {
			this.showPayPopup = !this.showPayPopup;
		},
		/**
		 * 发起付款请求
		 */
		payment(orderId, payType) {
			this.onTogglePayPopup();
			// 显示loading
			uni.showLoading({
				title: '正在处理...'
			});
			const postData = {
				order_id: orderId,
				payType: payType,
				return_url: HTML_DOMAIN + '/pages/order/index'
			};
			this.$u.api.payOrder(postData).then(res => {
				if (res.code === -10) {
					Tools.showError(result.msg);
					return false;
				}
				Payment.pay(
					res.data.payment,
					res.data.pay_type,
					() => {
						this.getOrderList(this.dataType);
					},
					msg => {}
				);
			});
		},
		/**
		 * 确认收货
		 */
		receipt(e) {
			let order_id = e.currentTarget.dataset.id;
			wx.showModal({
				title: '提示',
				content: '确认收到商品？',
				success: o => {
					if (o.confirm) {
						this.$u.api.receiptOrder(order_id).then(res => {
							this.getOrderList(this.dataType);
						});
					}
				}
			});
		},
		/**
		 * 跳转订单详情页
		 */
		navigateToDetail(e) {
			let order_id = e.currentTarget.dataset.id;
			this.navigateTo({
				url: '/pageShop/order/detail?order_id=' + order_id
			});
		},
		/**
		 * 查看核销二维码
		 */
		onExtractQRCode(e) {
			let order_id = e.currentTarget.dataset.id;
			// 调用后台api获取核销二维码
			uni.showLoading({
				title: '加载中'
			});
			this.$u.api.getOrderQrcode(order_id).then(res => {
				this.QRCodeImage = res.data.qrcode;
				// 显示核销二维码
				this.onToggleQRCodePopup();
			});
		},
		/**
		 * 核销码弹出层
		 */
		onToggleQRCodePopup() {
			this.showQRCodePopup = !this.showQRCodePopup;
		},
		/**
		 * 订单评价
		 */
		comment(e) {
			let order_id = e.currentTarget.dataset.id;
			this.navigateTo({
				url: '/pageShop/order/comment/comment?order_id=' + order_id
			});
		}
	}
};
</script>

<style lang="scss">
.header {
	width: 100%;
	height: 88rpx;
	border-bottom: 1rpx solid #e4e4e4;
	display: flex;
	background: #fff;
	line-height: 88rpx;
	position: fixed;
	top: var(--window-top);
	z-index: 3;
}

.header view {
	flex: 1;
	text-align: center;
	padding: 0 15rpx;
}

.header view text {
	display: block;
}

.header view.active {
	color: $theme-main-color;
}

.header view.active text {
	border-bottom: 1px solid $theme-main-color;
}

.order-box {
	padding: 0 15px;
}

.order-header {
	/* border-bottom: 1rpx solid #e4e4e4; */
	height: 80rpx;
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.order-header .title {
	font-size: 30rpx;
	color: #333;
}

.order-header .cont {
	/* display: block; */
	font-size: 30rpx;
	color: #fd4a5f;
}

.goods-detail-box {
	overflow: hidden;
}
.order-cont {
	padding: 15rpx 0;
	justify-content: space-between;
}

.order-cont .order-num text,
.order-time text {
	color: #777;
	font-size: 26rpx;
}
.order-shop {
	padding: 16rpx 0 16rpx 0;
}

.order-shop-left,
.order-shop-left image {
	width: 160rpx;
	height: 160rpx;
	/* border: 1rpx solid #e4e4e4; */
	margin-right: 15rpx;
	display: inline-block;
	background: #fff;
}

.order-shop .num {
	float: right;
}

.order-btn {
	padding: 18rpx 0 18rpx 0;
}

.order-btn .order-left {
	display: flex;
	align-items: center;
	flex: 1;
}

.order-btn .order-left text,
.order-btn .order-left .text {
	font-size: 29rpx;
}

.order-btn .order-left .text {
	color: #888;
}

.order-btn .order-right {
	flex: 1.5;
}

.order-btn .order-right .btn-group {
	justify-content: flex-end;
}

.order-list-box .left {
	flex: 4;
	overflow: hidden;
	position: relative;
}

.order-list-box .right {
	flex: 1;
}

.goods-number {
	font-size: 28rpx;
	color: #777;
}

.btn-default {
	border-radius: 4px;
	border: 1rpx solid #ccc;
	padding: 6rpx 20rpx;
	font-size: 28rpx;
	color: #555;
	float: right;
}

.btn-main {
	border-radius: 4px;
	border: 1rpx solid #fd4a5f;
	padding: 6rpx 20rpx;
	font-size: 28rpx;
	color: #fd4a5f;
	margin-left: 10rpx;
	float: right;
}

.user-orderJtou {
	font-size: 24rpx;
}

.order-img_list {
	background: #fff;
	border-top: 1rpx solid #f1f1f1;
	border-bottom: 1rpx solid #f1f1f1;
}

.order-right-name {
	position: absolute;
	top: 50%;
	left: 180rpx;
	font-size: 30rpx;
	color: #555;
	width: 100%;
	margin-top: -46rpx;
	line-height: 1.6;
}

/* 弹出层 */

.pop-example__container {
	background: #fff;
	padding: 30rpx 40rpx;
	border-radius: 10rpx;
	min-width: 460rpx;
}

.pop-example__container .pop-title {
	text-align: center;
}

.pop-content {
	margin-top: 30rpx;
}

/* 弹出层：核销码 */

.pop-qrcode .pop-content image {
	display: block;
	width: 460rpx;
	height: 460rpx;
}

.goods-detail-item {
	border-radius: 14rpx;
	overflow: hidden;
	margin: 20rpx 20rpx 30rpx;
}
</style>
